package work5.Grades;

import Uti.ScannerUti;
import work5.CourseManagement.Course;
import work5.CourseManagement.CourseDao;
import work5.StudentInfManagement.Student;
import work5.StudentInfManagement.StudentDao;

import java.util.List;


public class GradesManage {
    public GradesManage() {
        while (true) {
            System.out.println("成绩管理");
            System.out.println("--------------------------------");
            System.out.println("学生选课————1");
            System.out.println("老师成绩录入————2");
            System.out.println("成绩查询————3");
            System.out.println("退出————0");
            int kk = ScannerUti.getInt();
            if (kk == 1) {
                studentSelectCourse();
            } else if (kk == 2) {
                inputGrade();
            } else if (kk == 3) {
                queryGradeByStudent();
            } else if (kk == 0) {
                break;
            }
        }
    }


    //学生选课
    public static void studentSelectCourse() {
        System.out.println("输入学号：");
        int id = ScannerUti.getInt();
        List<Student> students = StudentDao.find(new Student(id));
        if (students.size() == 0) {
            System.out.println("查无此人");
            return;
        }
        Student s = students.get(0);
        while (true) {
            System.out.println(s);
            System.out.println("--------已选课程------------");
            List<Course> selectCourses = GradeDao.quireSelectCourse(s, true);
            printCourses(selectCourses);
            System.out.println("-----------------------------");
            System.out.println("--------未选课程------------");
            List<Course> noSelectCourses = GradeDao.quireSelectCourse(s, false);
            printCourses(noSelectCourses);
            System.out.println("-----------------------------");
            System.out.println("输入课程编号：-1——退出");
            int kk = ScannerUti.getInt();
            if (kk == -1) {
                break;
            }
            //判断是否胡乱输入
            boolean flag = false;
            for (int i = 0; i < noSelectCourses.size(); i++) {
                if (noSelectCourses.get(i).getId() == kk) {
                    flag = true;
                    break;
                }
            }
            if (flag == false) continue;
            //插入数据
            GradeDao.insert(s, new Course(kk));

        }
    }

    public static void printCourses(List<Course> list) {
        for (Course s : list) {
            System.out.println(s);
        }
    }

    /*成绩录入*/
    public static void inputGrade() {
        //打印所有课程.
        List<Course> courses = CourseDao.find(new Course(0));
        printCourses(courses);
        int course_id;
        while (true) {
            System.out.println("输入课程编号");
            course_id = ScannerUti.getInt();
            //判读是否胡乱输入
            boolean flag = false;
            for (int i = 0; i < courses.size(); i++) {
                if (courses.get(i).getId() == course_id) {
                    flag = true;
                    break;
                }
            }
            if (flag == true) break;

        }
        // 显示该课程的所有成绩记录
        while (true) {
            List<Grade> grades = GradeDao.findGradesByCourse(new Course(course_id));
            if (grades.size() == 0) break;
            printCourseGrade(grades.get(0).getCourse(), grades);

            System.out.println("请输入学生编号:，-1退出");
            int s_id = ScannerUti.getInt();
            if (s_id == -1) break;
            Student student;
            List<Student> students = StudentDao.find(new Student(s_id));
            if (students.size() == 0) {
                System.out.println("输入有误");
                continue;
            }
            student = students.get(0);
            //添加成绩
            System.out.println("输入" + student.getName() + "的成绩：");
            double m = ScannerUti.getDouble();
            GradeDao.insert(new Grade(student, new Course(course_id), m));
        }
    }


    /*打印课程成绩表*/
    public static void printCourseGrade(Course c, List<Grade> results) {
        System.out.println("课程：" + c.getName());
        System.out.println("学号\t\t学生名字\t\t成绩");
        System.out.println("----------------------------------------");
        for (Grade g : results) {
            System.out.println(g.getStudent().getId() + "\t\t" + g.getStudent().getName() + "\t\t" + g.getGrade());
        }
        System.out.println("----------------------------------------");
    }

    /*打印学生成绩表*/
    public static void printStudentGrade(Student s, List<Grade> results) {
        System.out.println("学生：" + s.getName());
        System.out.println("课程编号\t\t课程名字\t\t成绩");
        System.out.println("----------------------------------------");
        double sum = 0;
        for (Grade g : results) {
            System.out.println(g.getCourse().getId() + "\t\t" + g.getCourse().getName() + "\t\t" + g.getGrade());
            if (g.getGrade() >= 60) {
                sum = sum + g.getCourse().getCredit();
            }
        }
        System.out.println("已修学分：" + sum);
        System.out.println("----------------------------------------");
    }

    public static void queryGradeByStudent(){
        System.out.println("请输入学号：");
        int s_id = ScannerUti.getInt();
        List<Student> students = StudentDao.find(new Student(s_id));
        if (students.size() == 0) {
            System.out.println("没有这个学生！！");
            return;
        }

        List<Grade> grades = GradeDao.findGradesByStudent(students.get(0));
        printStudentGrade(students.get(0), grades);

    }

}



